<template>
  <div class="navigation-container">
    <div class="navigation-header" :class="{ border: formProps.border }">
      <slot name="title">
        <div class="navigation-label" v-if="!hasTitle">
          <div class="common-doc"></div>
          <span>{{ formProps.label }}</span>
        </div>
      </slot>
      <div class="navigation-context">
        <slot name="suffix"></slot>
        <el-button
          v-if="showAddBtn"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="handleAdd"
          v-auth
          >新增</el-button
        >
        <el-button
          class="navigation-search-button"
          type="primary"
          icon="el-icon-search"
          @click="show = !show"
          v-if="formProps.hasSearch"
        ></el-button>
      </div>
    </div>
    <el-collapse-transition>
      <div v-if="show">
        <el-form
          :inline="true"
          ref="ruleForm"
          :label-width="formProps.labelWidth"
          v-model="formValues"
          class="navigation-form"
        >
          <template v-for="(item, index) in formProps.formList">
            <el-form-item
              v-if="item.type === 'input'"
              :key="index"
              :label="`${item.label}`"
            >
              <el-input
                v-model="formValues[item.prop]"
                clearable
                :placeholder="`请输入${item.label}`"
              ></el-input>
            </el-form-item>
            <el-form-item
              v-if="item.type === 'select'"
              :key="index"
              :label="`${item.label}`"
            >
              <el-select v-model="formValues[item.prop]" clearable>
                <el-option
                  v-for="selectItem in item.dataSource"
                  :key="selectItem[item.sourceOption.value]"
                  :label="selectItem[item.sourceOption.label]"
                  :value="selectItem[item.sourceOption.value]"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              v-if="item.type === 'cascader'"
              :key="index"
              :label="`${item.label}`"
            >
              <el-cascader
                v-model="formValues[item.prop]"
                :options="item.dataSource"
                :props="defaultProps"
                clearable
              ></el-cascader>
            </el-form-item>
            <el-form-item
              v-if="item.type === 'datePicker'"
              :key="index"
              :label="`${item.label}`"
            >
              <el-date-picker
                v-model="formValues[item.prop]"
                clearable
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </template>
          <el-form-item class="navigation-formbtn">
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-collapse-transition>
  </div>
</template>
<script>
import { cloneDeep } from "lodash";
export default {
  props: {
    dataRequest: {
      type: Function,
    },
    showAddBtn: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      show: false,
      hasTitle: false,
      formProps: {
        label: "默认",
        formItem: [],
        labelWidth: "75px",
        hasSearch: false,
        border: true,
      },
      formValues: {},
      formValuesCopy: {},
      defaultProps: {
        children: "descendants",
        label: "name",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  mounted() {
    //转布尔值
    this.hasTitle = !!this.$slots.title;
  },
  methods: {
    init({
      formItem = [],
      formValues = {},
      label = "默认",
      labelWidth = "75px",
      hasSearch = true,
      border = true,
    }) {
      const formList = formItem.map((i) => {
        const defaultValue = i.type === "cascader" ? "" : [];
        const sourceOption = {
          label: "label",
          value: "value",
        };
        return {
          label: i.label || "默认",
          type: i.type || "input",
          prop: i.prop || "",
          labelWidth: i.labelWidth,
          dataSource: i.dataSource || [],
          defaultValue: i.defaultValue || defaultValue,
          sourceOption: i.sourceOption || sourceOption,
        };
      });
      this.formProps = {
        hasSearch,
        label,
        labelWidth,
        formList,
        border,
      };
      this.formValues = cloneDeep(formValues);
      this.formValuesCopy = cloneDeep(formValues);
    },
    reset() {
      this.formValues = cloneDeep(this.formValuesCopy);
      this.onSubmit();
    },
    resetFields() {
      this.formValues = {};
    },
    handleAdd() {
      this.$emit("add");
    },
    onSubmit() {
      const params = {};
      const filterList = [undefined, null, ""];
      //过滤没值

      for (const key in this.formValues) {
        const i = this.formValues[key];
        if (!filterList.includes(i)) {
          //首字母大写
          // const str = key.replace(key[0], key[0].toUpperCase());
          // params[`like${str}`] = this.formValues[key];
          params[key] = i;
        }
      }
      this.dataRequest(params);
      this.$emit("requestTableData");
    },
  },
};
</script>
<style lang="scss" scope>
@import "@/style/common.scss";
.navigation-container {
  .navigation-header {
    height: 50px;
    background: #f3f3f3;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
    &.border {
      // border: 1px solid #ccc;
    }
    .navigation-label {
      display: flex;
      font-size: 16px;
      color: rgb(102, 100, 100);
    }
    .navigation-context {
      display: flex;
      .navigation-search-button {
        margin-left: 5px;
      }
    }
  }
  .navigation-form {
    margin-top: 15px;
    .el-form-item {
      margin-right: 20px;
    }

    .navigation-formbtn {
      margin-left: 20px;
    }
  }
}
</style>
